package com.example.ywj.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.example.ywj.myapplication.fragment.OtherFragment;
import com.example.ywj.myapplication.weixin.IndexActivity;
import com.example.ywj.myapplication.weixin2.HomeActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
    public void initView(){
        Button btn = findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                //实例化
                OtherFragment otherFragment = new OtherFragment();
                //  2.获得FragmentManager
                FragmentManager fm = getSupportFragmentManager();
                // 3.开启事务
                FragmentTransaction transaction = fm.beginTransaction();
                // 4.动态的添加
                transaction.replace(R.id.otherContent,otherFragment,"otherFragment");
                // 5.提交
                transaction.commit();
            }
        });
        Button btn2 = findViewById(R.id.button1);
        btn2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,IndexActivity.class);
                startActivity(intent);
            }
        });
        Button btn3 = findViewById(R.id.button2);
        btn3.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(MainActivity.this,HomeActivity.class);
                startActivity(intent1);
            }
        });

    }
}
